package GUI;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import modelo.Urgencia;
import persistencia.UrgenciasDBManager;

/**
 *
 * @author Administrator
 */
public class JPanelModificarUrgencia extends javax.swing.JPanel {

    /**
     * Creates new form JPanelModificarUrgencia
     */
    public JPanelModificarUrgencia() {
        initComponents();
        
        this.setBackground(Color.LIGHT_GRAY);
        this.setVisible(true);
        
        UrgenciasDBManager.rellenarComboBoxUrgencia(jcbUrgencia);
        UrgenciasDBManager.rellenarComboBoxPA(jcbPA);
        
            txtFechaUrgencia.addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
          char c = e.getKeyChar();
          if (!((c >= '0') && (c <= '9') ||
             (c == KeyEvent.VK_BACK_SPACE) ||
             (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_SLASH)))        
          {
            JOptionPane.showMessageDialog(null, "Por favor, introduzca una fecha válida.");
            e.consume();
          }
        }
      });
    
    
        txtFechaAlta.addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
          char c = e.getKeyChar();
          if (!((c >= '0') && (c <= '9') ||
             (c == KeyEvent.VK_BACK_SPACE) ||
             (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_SLASH)))        
          {
            JOptionPane.showMessageDialog(null, "Por favor, introduzca una fecha válida.");
            e.consume();
          }
        }
      });
    }
    
    public void limpiar(){
        txtFechaAlta.setText("");
        txtFechaUrgencia.setText("");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btnModificar = new javax.swing.JButton();
        txtFechaUrgencia = new javax.swing.JTextField();
        txtFechaAlta = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jcbUrgencia = new javax.swing.JComboBox();
        jcbPA = new javax.swing.JComboBox();

        jLabel4.setFont(new java.awt.Font("Verdana", 3, 36)); // NOI18N
        jLabel4.setText("MODIFICAR URGENCIA");

        jLabel1.setText("Fecha Urgencia:");

        jLabel2.setText("Fecha Alta:");

        jLabel3.setText("Proceso asistencial:");

        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        jLabel5.setText("Urgencia:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(152, 152, 152))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnModificar)
                        .addGap(356, 356, 356))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtFechaUrgencia, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtFechaAlta, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jcbUrgencia, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jcbPA, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(234, 234, 234))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbUrgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtFechaUrgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtFechaAlta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jcbPA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addComponent(btnModificar)
                .addContainerGap(35, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        // TODO add your handling code here:
        if(txtFechaUrgencia.getText().compareTo("")==0){
            JOptionPane.showMessageDialog(null, "Por favor, el campo fecha urgencia es obligatorio.");
        }else if(txtFechaAlta.getText().compareTo("")==0){
             JOptionPane.showMessageDialog(null, "Por favor, el campo fecha alta es obligatorio.");
        }else{
            Urgencia urgVieja=new Urgencia((int) jcbUrgencia.getSelectedItem());
            Urgencia urgNueva=new Urgencia((int) jcbUrgencia.getSelectedItem(), txtFechaUrgencia.getText(), txtFechaAlta.getText(), (int) jcbPA.getSelectedItem());
            try {
                UrgenciasDBManager.updateUrgencia(urgVieja, urgNueva);
                UrgenciasDBManager.rellenarComboBoxUrgencia(jcbUrgencia);
                UrgenciasDBManager.rellenarComboBoxPA(jcbPA);
                JOptionPane.showMessageDialog(null, "Urgencia modificada correctamente.", "Información", JOptionPane.INFORMATION_MESSAGE);
                limpiar();
            } catch (SQLException ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "Se ha producido un error.", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
        
    }//GEN-LAST:event_btnModificarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnModificar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JComboBox jcbPA;
    private javax.swing.JComboBox jcbUrgencia;
    private javax.swing.JTextField txtFechaAlta;
    private javax.swing.JTextField txtFechaUrgencia;
    // End of variables declaration//GEN-END:variables
}
